package com.ruoyi.web.controller.ora;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.enums.dict.AssetsType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.bean.Builder;
import com.ruoyi.ora.domain.*;
import com.ruoyi.ora.service.IVoiceMemoirService;
import com.ruoyi.ora.service.IVoiceRecordService;
import com.ruoyi.ora.service.IVoiceStoryService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.ora.service.IAssetsRecycleService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 回收站Controller
 * 
 * @author liuwp
 * @date 2025-05-20
 */
@RestController
@RequestMapping("/ora/assets_recycle")
public class AssetsRecycleController extends BaseController
{
    @Autowired
    private IAssetsRecycleService assetsRecycleService;
    @Autowired
    private IVoiceRecordService voiceRecordService;
    @Autowired
    private IVoiceMemoirService voiceMemoirService;
    @Autowired
    private IVoiceStoryService voiceStoryService;

    /**
     * 查询回收站列表
     */
    @PreAuthorize("@ss.hasPermi('ora:assets_recycle:list')")
    @GetMapping("/list")
    public TableDataInfo list(AssetsRecycle assetsRecycle)
    {
        startPage();
        List<AssetsRecycle> list = assetsRecycleService.selectAssetsRecycleList(assetsRecycle);
        return getDataTable(list);
    }

    /**
     * 导出回收站列表
     */
    @PreAuthorize("@ss.hasPermi('ora:assets_recycle:export')")
    @Log(title = "回收站", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AssetsRecycle assetsRecycle)
    {
        List<AssetsRecycle> list = assetsRecycleService.selectAssetsRecycleList(assetsRecycle);
        ExcelUtil<AssetsRecycle> util = new ExcelUtil<AssetsRecycle>(AssetsRecycle.class);
        util.exportExcel(response, list, "回收站数据");
    }

    /**
     * 获取回收站详细信息
     */
    @PreAuthorize("@ss.hasPermi('ora:assets_recycle:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(assetsRecycleService.selectAssetsRecycleById(id));
    }

    /**
     * 新增回收站
     */
    @PreAuthorize("@ss.hasPermi('ora:assets_recycle:add')")
    @Log(title = "回收站", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AssetsRecycle assetsRecycle)
    {
        return toAjax(assetsRecycleService.insertAssetsRecycle(assetsRecycle));
    }

    /**
     * 修改回收站
     */
    @PreAuthorize("@ss.hasPermi('ora:assets_recycle:edit')")
    @Log(title = "回收站", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AssetsRecycle assetsRecycle)
    {
        return toAjax(assetsRecycleService.updateAssetsRecycle(assetsRecycle));
    }

    /**
     * 删除回收站
     */
    @PreAuthorize("@ss.hasPermi('ora:assets_recycle:remove')")
    @Log(title = "回收站", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        int delCount = 0;
        for (Long id : ids) {
            delCount += assetsRecycleService.updateAssetsRecycle(Builder.of(AssetsRecycle::new).with(AssetsRecycle::setId, id).with(AssetsRecycle::setDeleteTime, new Date()).build());
        }
        return toAjax(delCount);
//        return toAjax(assetsRecycleService.deleteAssetsRecycleByIds(ids));
    }

    @PreAuthorize("@ss.hasPermi('ora:sos_roster:list')")
    @GetMapping("/query")
    public AjaxResult query(AssetsRecycle assetsRecycle)
    {
        assetsRecycle.setUserId(SecurityUtils.getUserId());
        List<AssetsRecycle> list = assetsRecycleService.selectAssetsRecycleList(assetsRecycle);
        for (AssetsRecycle recycle : list) {
            if (recycle.getAssetsType().intValue() == AssetsType.VOICE.getValue()) {
                recycle.setVoiceRecord(voiceRecordService.selectVoiceRecordById(recycle.getAssetsId()));
            }else if (recycle.getAssetsType().intValue() == AssetsType.MEMORI.getValue()) {
                recycle.setVoiceMemoir(voiceMemoirService.selectVoiceMemoirById(recycle.getAssetsId()));
            }else if (recycle.getAssetsType().intValue() == AssetsType.STROY.getValue()) {
                recycle.setVoiceStory(voiceStoryService.selectVoiceStoryById(recycle.getAssetsId()));
            }
        }
        return AjaxResult.success(list);
    }

    @GetMapping("/restore")
    public AjaxResult restore(Long id)
    {
        AssetsRecycle assetsRecycle = assetsRecycleService.selectAssetsRecycleById(id);
        if (assetsRecycle != null) {
            assetsRecycleService.updateAssetsRecycle(Builder.of(AssetsRecycle::new).with(AssetsRecycle::setId, id).with(AssetsRecycle::setRestoreTime, new Date()).build());
            int result = 0;
            if (assetsRecycle.getAssetsType().intValue() == AssetsType.VOICE.getValue()) {
                result = voiceRecordService.updateVoiceRecord(Builder.of(VoiceRecord::new).with(VoiceRecord::setId, assetsRecycle.getAssetsId()).with(VoiceRecord::setDeleteTime, null).build());
            }else if (assetsRecycle.getAssetsType().intValue() == AssetsType.MEMORI.getValue()) {
                result = voiceMemoirService.updateVoiceMemoir(Builder.of(VoiceMemoir::new).with(VoiceMemoir::setId, assetsRecycle.getAssetsId()).with(VoiceMemoir::setDeleteTime, null).build());
            }else if (assetsRecycle.getAssetsType().intValue() == AssetsType.STROY.getValue()) {
                result = voiceStoryService.updateVoiceStory(Builder.of(VoiceStory::new).with(VoiceStory::setId, assetsRecycle.getAssetsId()).with(VoiceStory::setDeleteTime, null).build());
            }
            return toAjax(result);
        }
        return AjaxResult.error();
    }

}
